package hhq.demo.ctrl;

import java.util.List;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import hhq.demo.mapper.BookMarkFolderMapper;
import hhq.demo.mapper.BookMarkItemMapper;
import hhq.demo.pojo.BookMarkFolder;
import hhq.demo.pojo.BookMarkItem;
import hhq.demo.utils.ResJson;

@RestController
public class BookMarkFolderController {

    @Resource
    BookMarkFolderMapper fathMap;
    @Resource
    BookMarkItemMapper sm;
    
    @RequestMapping(value= "/getAllFather")
    public String getAllFather(){
        try {
            List<BookMarkItem> son_s = sm.findAll();
            List<BookMarkFolder> father_s = fathMap.findAll();
            JSONObject big = new JSONObject();
            JSONArray jarr = new JSONArray();
            
            for( BookMarkFolder f: father_s){
                for( BookMarkItem s: son_s){
                    if( f.getId() == s.getFolder_id() ){
                        f.getSons().add(s);
                    }
                }
                JSONObject small = (JSONObject) JSONObject.toJSON(f);
                jarr.add(small);
            }
            big.put("arr", jarr);
 
            return ResJson.getString(20, "成功", big);
            
            
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return ResJson.getString(40, "getAllFather 出错");
    }

    @RequestMapping(value="/updateFather", method = RequestMethod.POST)
    public String updateFather(@RequestBody BookMarkFolder father){
        try {
           fathMap.update(father);
            return ResJson.getString(20, "成功");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return ResJson.getString(40, "updateFather 出错");
    }
    @RequestMapping(value= "/addFather/{name}")
    public String addFather(@PathVariable("name") String name){
        try {
            BookMarkFolder father = new BookMarkFolder();
            father.setId(0);
            father.setName(name);
            fathMap.insert(father);
            return ResJson.getString(20, "成功");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return ResJson.getString(40, "updateFather 出错");
    }
    
    @RequestMapping(value= "/deleteFather/{fid}")
    public String deleteFather(@PathVariable("fid") int fid){
        try {
            sm.deleteByFatherId(fid);
            fathMap.delete(fid);
            return ResJson.getString(20, "成功");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return ResJson.getString(40, "updateFather 出错");
    }

}